package cz.proks.easyhelpdesk.gae.api.impl.ws;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cz.proks.easyhelpdesk.api.HelpdeskMemberApi;
import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.exception.ApiException;
import cz.proks.easyhelpdesk.api.model.HelpdeskMember;
import cz.proks.easyhelpdesk.api.model.HelpdeskRole;

@Controller(Configuration.WS.HELPDESK_MEMBER_API)
@RequestMapping(Configuration.WS.HELPDESK_MEMBER_BASE_URL)
public class HelpdeskMemberApiImpl extends BaseApiImpl<HelpdeskMember> implements HelpdeskMemberApi {    
    private final Logger log = LoggerFactory.getLogger(getClass());
    
    @Autowired
    @Qualifier(Configuration.Core.WS.HELPDESK_MEMBER_API)
    private HelpdeskMemberApi coreApi;
    
    @Override
    public HelpdeskMemberApi getCoreApi() {
        return coreApi;
    }

    @Override
    @RequestMapping(value = "/getForUser/{userId}/ForHelpdesk/{helpdeskId}", method = RequestMethod.GET)
    @ResponseBody
    public HelpdeskMember getForUserAndHelpdesk(@PathVariable Long userId, @PathVariable Long helpdeskId) throws ApiException {
        log.debug("getForUserAndHelpdesk():: Start with userId '{}', helpdeskId '{}'", userId, helpdeskId);
        
        HelpdeskMember helpdeskMember = getCoreApi().getForUserAndHelpdesk(userId, helpdeskId);
        
        log.debug("getForUserAndHelpdesk():: End with '{}'", helpdeskMember);
        return helpdeskMember;
    }

    @Override
    @RequestMapping(value = "/listForUser/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public List<HelpdeskMember> listForUser(@PathVariable Long userId) throws ApiException {
        log.debug("listForUser():: Start with userId '{}', helpdesk '{}'", userId);

        List<HelpdeskMember> helpdeskMembers = getCoreApi().listForUser(userId);

        log.debug("listForUser():: End with {}", (helpdeskMembers == null ? "null list" : "list of " + helpdeskMembers.size() + " elements"));
        return helpdeskMembers;
    }
    
    @Override
    @RequestMapping(value = "/listForHelpdesk/{helpdeskId}", method = RequestMethod.GET)
    @ResponseBody
    public List<HelpdeskMember> listForHelpdesk(@PathVariable Long helpdeskId) throws ApiException {
        log.debug("listForHelpdesk():: Start with userId '{}', helpdesk '{}'", helpdeskId);

        List<HelpdeskMember> helpdeskMembers = getCoreApi().listForHelpdesk(helpdeskId);

        log.debug("listForHelpdesk():: End with {}", (helpdeskMembers == null ? "null list" : "list of " + helpdeskMembers.size() + " elements"));
        return helpdeskMembers;
    }       
    
    @Override
    @RequestMapping(value = "/{id}/addHelpdeskRole/{helpdeskRole}", method = RequestMethod.PUT)
    @ResponseBody
    public void addHelpdeskRole(@PathVariable Long id, @PathVariable HelpdeskRole helpdeskRole) throws ApiException {
        log.debug("addHelpdeskRole():: Start with id '{}', helpdeskRole '{}'", id, helpdeskRole);

        getCoreApi().addHelpdeskRole(id, helpdeskRole);

        log.debug("addHelpdeskRole():: Exit");        
    }

    @Override
    @RequestMapping(value = "/{id}/removeHelpdeskRole/{helpdeskRole}", method = RequestMethod.DELETE)
    @ResponseBody
    public void removeHelpdeskRole(@PathVariable Long id, @PathVariable HelpdeskRole helpdeskRole) throws ApiException {
        log.debug("removeHelpdeskRole():: Start with id '{}', helpdeskRole '{}'", id, helpdeskRole);

        getCoreApi().removeHelpdeskRole(id, helpdeskRole);

        log.debug("removeHelpdeskRole():: Exit");
    }        
}
